﻿
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Linq;
using System.Collections.Generic;
using System;
using AutoMapper;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore.Internal;


using System.Linq.Expressions;
using LinqKit;
using Ccteg.Common;
using CCcteg.Common;
using Ccteg.DB;
using Cari.IBP.Platform.Sidecar;

namespace Cctegaq.Api.Controllers
{
    /// <summary>
    /// 风险管控
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RSKController : IbpControllerBase
    {
        private readonly AppAuthenticationSettings _appSettings;
        private readonly MySqlDbContext _dbContext;
        private readonly IMapper _mapper;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appSettings"></param>
        /// <param name="dbContext"></param>
        /// <param name="mapper"></param>
        public RSKController(IOptions<AppAuthenticationSettings> appSettings, MySqlDbContext dbContext, IMapper mapper)
        {
            _appSettings = appSettings.Value;
            _dbContext = dbContext;
            _mapper = mapper;
        }

        #region 风险管控
        /// <summary>
        /// 风险管控查询
        /// </summary>
        /// <param name="companyno">二级单位编码</param>
        /// <param name="level">风险等级</param>
        ///  <param name="major">风险专业</param>
        /// <param name="risktype">风险类型</param>
        /// <param name="hazard">危险源</param>
        /// <param name="description">风险描述</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public ResponseResultModel<IEnumerable<Rsk_rsk_ViewModel>> RskRskSearch(string companyno, string level, string major, string risktype, string hazard, string description, int pageindex = 1, int pagesize = 10)
        {
            using (_dbContext)
            {


                var response = ResponseModelFactory<IEnumerable<Rsk_rsk_ViewModel>>.CreateResultInstance;
                Expression<Func<rsk_rsk, bool>> where = p => true;


                where = where.CheckNullAnd(x => x.CompanyNo == companyno, companyno);
                where = where.CheckNullAnd(x => x.Level == level, level);
                where = where.CheckNullAnd(x => x.Major == major, major);
                where = where.CheckNullAnd(x => x.RiskType == risktype, risktype);
                where = where.CheckNullAnd(x => x.Hazard.Contains(hazard), hazard);
                where = where.CheckNullAnd(x => x.Description.Contains(description), description);

                var query = _dbContext.Rsk_Rsk.AsQueryable();

                query = query.Where(where.Expand()).OrderByDescending(x => x.Id);

                var list = query.Paged(pageindex, pagesize).ToList();
                var totalCount = query.Count();
                var data = list.Select(_mapper.Map<rsk_rsk, Rsk_rsk_ViewModel>);
                response.SetData(data, totalCount);
                return response;
            }
        }
        /// <summary>
        /// 风险管控---管控措施
        /// </summary>
        /// <param name="riskbaseno">风险基元编码</param>
        /// <param name="companyno">二级单位编码</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public ResponseResultModel<IEnumerable<Rsk_measure_ViewModel>> GetRskMeasureSearch([Required] string riskbaseno, [Required] string companyno, int pageindex = 1, int pagesize = 10)
        {

            using (_dbContext)
            {
                var response = ResponseModelFactory<IEnumerable<Rsk_measure_ViewModel>>.CreateResultInstance;
                var query = _dbContext.Rsk_Measure.Where(x => x.RiskBaseNo == riskbaseno && x.CompanyNo == companyno);
                query = query.OrderByDescending(x => x.Id);
                var list = query.Paged(pageindex, pagesize).ToList();
                var totalCount = query.Count();
                var data = list.Select(_mapper.Map<rsk_measure, Rsk_measure_ViewModel>);
                response.SetData(data, totalCount);
                return response;
            }
        }

        #endregion
    }
}